package kpi.java_training.testing.commands;


import kpi.java_training.testing.Testing;

import java.util.ArrayList;
import java.util.List;

/**
 *  Class which organize list of commands
 */
public class CommandList {
    private final int NOT_COMMAND = -1;
    private List<Command> commands;

    public CommandList() {
        commands = new ArrayList<Command>();
    }

    public void addStandardCommands(Testing test) {
        Command command = new BreakCommand(test);
        add(command);
        command = new UndoCommand(test);
        add(command);
    }

    private void add(Command command) {
        commands.add(command);
    }

    private int indexOfCommand(String string) {
        for (int i = 0; i < commands.size(); i++) {
            if (commands.get(i).itsMe(string)) {
                return i;
            }
        }
        return NOT_COMMAND;
    }

    public String executeCommand(String string) {
        int index = indexOfCommand(string);
        if (index != NOT_COMMAND) {
            commands.get(index).execute();
            return commands.get(index).toString();
        }
        return "";
    }
}
